class Solution {
public:
    string addBinary(string a, string b) {
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        if(a.size() > b.size())
        {
            std::string tmp = b;
            b = a;
            a = tmp;
        }
        int flag = 0;
        std::string res = "";
        for(int i = 0; i < b.size(); i++)
        {
            int mid;
            if(i < a.size())
            {
                mid = a[i] + b[i] + flag - 96;
            }
            else
            {
                mid = b[i] + flag - 48;
            }
            if(mid > 1)
            {
                res += std::to_string(mid - 2);
                flag = 1;
            }
            else
            {
                res += std::to_string(mid);
                flag = 0;
            }
        }
        if(flag)
        {
            res += std::to_string(1);
        }
        reverse(res.begin(), res.end());
        return res;
    }
};